<template>
  <view class="">
    <uni-popup ref="mypopup" type="center">
      <view class="popup">
        <view class="popup-close" @click="close()"></view>
        <view class="popup-title">{{ popup.title }}</view>
        <view class="popup-text">{{ popup.text }}</view>
        <view class="popup-button" @click="jump()">{{ popup.button }}</view>
      </view>
    </uni-popup>
  </view>
</template>
<script>
import { getUnreadInformation, getUnreadTask } from "@/api/index";
import {
  getframe,
  setframe,
  removeframe,
  gettime0,
  settime0,
  removetime0,
  gettime1,
  settime1,
  removetime1,
  gettime2,
  settime2,
  removetime2,
} from "@/utils/auth3";
export default {
  name: "Prompt",
  props: {},
  data() {
    return {
      popup: {
        title: "您的提问已收到答复",
        text: "请前往【我的】-【我的提问】处查看答复情况。",
        button: "立即前往查看",
        url: "pages/mine/index",
        href: "",
      },
    };
  },
  computed: {
    refresh2() {
      return this.$store.state.user.refresh2;
    },
  },
  watch: {
    refresh2: {
      immediate: true,
      handler: function (newval) {},
    },
  },
  created() {
    this.Initialize();
  },
  methods: {
    Initialize() {
      this.href = location.href;
      let time = new Date() - gettime0() - 0;
      if (this.$store.state.user.promptflag) {
        //首次
        time = 300000;
        this.$store.state.user.promptflag = false;
      }
      let frame = 300000 - time;
      // console.log("gettime0()",gettime0())
      // console.log("new Date()",new Date())
      // console.log("frame",frame)
      settime1("");
      settime2("");
      setTimeout(this.countDown, frame);
    },
    countDown() {
      console.log("getframe()", getframe());
      getUnreadInformation().then((res) => {
        if (
          this.$store.state.user.prompt.tobeGetQuestions > 0 ||
          this.$store.state.user.prompt.unreadCounselingNum > 0 ||
          this.$store.state.user.prompt.unreadQuestionsNum > 0
        ) {
          getUnreadTask().then((res) => {
            // let a = new Date('2022-10-25 15:20:36').getTime()
            let data1 = res.unreadQuestionsList;
            let data2 = res.unreadCounselingList;
            let time1 = gettime1();
            let time2 = gettime2();
            if (data1.length > 0) {
              if (time1 && time1 == data1[0].lastAnswerTime) {
              } else {
                settime1(data1[0].lastAnswerTime);
                this.$store.state.user.switch = 0;
                this.popup = {
                  title: "您的提问已收到答复",
                  text: "请前往【我的】-【我的提问】处查看答复情况。",
                  button: "立即前往查看",
                  url: "pages/mine/index",
                  href: "",
                };
                this.open();
              }
            } else if (data2.length > 0) {
              if (time2 && time2 == data2[0].lastAnswerTime) {
              } else {
                settime2(data2[0].lastAnswerTime);
                this.$store.state.user.switch = 1;
                this.popup = {
                  title: "您的咨询已收到答复",
                  text: "请前往【我的】-【我的咨询】处查看答复情况。",
                  button: "立即前往查看",
                  url: "pages/mine/index",
                  href: "",
                };
                this.open();
              }
            }
          });
        }
        this.$store.state.user.prompt.tobeGetQuestions = res.tobeGetQuestions;
        this.$store.state.user.prompt.unreadCounselingNum =
          res.unreadCounselingNum;
        this.$store.state.user.prompt.unreadQuestionsNum =
          res.unreadQuestionsNum;
        if (this.href == location.href) {
          setTimeout(this.countDown, 300000);
        }
      });
      let time = new Date();
      console.log("ptime", time);
      settime0(time);
    },
    open() {
      this.$refs.mypopup.open("center");
    },
    close() {
      this.$refs.mypopup.close();
    },
    jump() {
      this.$refs.mypopup.close();
      this.$router.push(this.popup.url);
    },
  },
  beforeDestroy() {},
};
</script>
<style lang="scss" scoped>
.popup {
  width: 60vw;
  height: 40vw;
  background: #ffffff;
  position: absolute;
  margin-left: -30vw;
  margin-top: -20vw;
  border-radius: 2.2vw;

  display: grid;
  justify-content: center;
  align-items: center;
  grid-template-rows: 5vw 5vw 15vw 15vw;
}
.popup-close {
  margin: 2vw 0 0 55vw;
  width: 3vw;
  height: 3vw;
  background: url(../static/index/close.svg);
  background-size: 100% 100%;
  cursor: pointer;
}
.popup-title {
  display: grid;
  justify-content: center;
  align-items: center;
  font-weight: 600;
  font-size: 4vw;
}
.popup-text {
  margin: 0 5vw 0 05vw;
  width: 50vw;
  display: grid;
  justify-content: center;
  align-items: center;
  font-size: 3.4vw;
}
.popup-button {
  margin: 0 5vw 0 5vw;
  width: 50vw;
  height: 8vw;
  border-radius: 4vw;
  display: grid;
  justify-content: center;
  align-items: center;
  color: #ffffff;
  background: #1492ff;
  cursor: pointer;
}
</style>
